package com.moon.icoresso.strategy.impl;

import com.moon.icoresso.Service.OauthRedisService;
import com.moon.icoresso.Service.OauthService;
import com.moon.icoresso.pojo.bo.OauthTokenHandleBO;
import com.moon.icoresso.pojo.dto.OauthTokenDTO;
import com.moon.icoresso.pojo.dto.OauthTokenParam;
import com.moon.icoresso.strategy.OauthStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.moon.icoresso.pojo.constants.Constants4Oauth.OAUTH_CLIENT_GRANT_TYPE;

/**
 * @ClassName OauthClientStrategy
 * @Description
 * @Author menbbo
 * @Date 2021/2/19 20:46
 * @Version V1.0
 **/
@Component(value = OAUTH_CLIENT_GRANT_TYPE)
public class OauthClientStrategy implements OauthStrategy {
    @Autowired
    private OauthService oauthService;
    @Autowired
    private OauthRedisService oauthRedisService;
    @Override
    public void checkParam(OauthTokenParam oauthTokenParam, OauthTokenHandleBO oauthTokenHandleBO) {
        oauthService.checkClientAndClientSecret(oauthTokenParam.getClientId(),oauthTokenParam.getClientSecret());
    }

    @Override
    public OauthTokenDTO handle(OauthTokenParam oauthTokenParam, OauthTokenHandleBO oauthTokenHandleBO) {
        OauthTokenDTO oauthTokenDTO = oauthService.generateToken(true);
        //将token保存
        oauthRedisService.saveAccessToken(oauthTokenParam.getGrantType(),oauthTokenParam.getClientId(),oauthTokenDTO.getAccessToken(),oauthTokenHandleBO.getOauthUserInfo());
        oauthRedisService.saveRefreshToken(oauthTokenDTO.getRefreshToken(),oauthTokenParam.getGrantType(),oauthTokenParam.getClientId(),oauthTokenHandleBO.getOauthUserInfo());
        //删除授权码
        return oauthTokenDTO;
    }
}